import { Component, OnInit, Input } from '@angular/core';
import { AppGlobalService } from 'src/app/shared/service/app-global.service';
import { HttpService } from 'src/app/shared/service/http.service';
@Component({
  selector: 'app-statistic-sales',
  templateUrl: './statistic-sales.component.html',
  styleUrls: ['./statistic-sales.component.scss'],
})
export class StatisticSalesComponent implements OnInit {
  @Input() tagData: any;
  params: any;
  pageNo = 1;
  pageTotal: any;
  constructor(
    public appGlobalService: AppGlobalService,
    public httpService: HttpService,
  ) { }

  ngOnInit() {
    this.pageTotal = this.tagData.pageTotal;
  }

  openPage(link) {
    this.appGlobalService.openPage(link);
  }
  refreshData(submit) {
    this.params = { id: '', content: {}, sessionId: '' };
    this.params.id = submit.id;
    this.params.content = { order: submit.order, pageNo: 1 };
    this.params.sessionId = localStorage.getItem("sessionId");

    this.httpService.post('application/submit', this.params, res => {
      if (res.success) {
        if (res.data.submit) {
          this.params = res.data.submit;
        }
        this.tagData.data = [];
        const result = res.data.data;
        this.tagData.data = result;
      }
    },
      err => {
        // 请求数据失败
        this.tagData.data = [];
      });
  }

    // 上滑加载
    loadMoreData(event) {
      if (this.pageNo >= this.pageTotal) {
        event.target.disabled = true;
        // this.appGlobalService.showToast("没有数据了...", 1000, "middle");
        // event.complete();
        return;
      }
      // // 请求下一个页面
      this.pageNo = this.pageNo + 1;
      // // 参数
      this.params.content["pageNo"] = this.pageNo;
      // 请求获取新数据
      this.httpService.post(
        "application/submit",
        this.params,
        res => {
          // 请求数据成功
          if (res.success) {
            const result = res.data.data;
            this.tagData.data = result;
          } else {
            // 请求失败，更新当前页数
            this.pageNo = this.pageNo - 1;
            // 请求数据失败
            this.appGlobalService.showToast(res.message, 2000, "middle");
          }
          event.target.complete();
        },
        err => {
          // 请求失败，更新当前页数
          this.pageNo = this.pageNo - 1;
          // 请求数据失败
          this.appGlobalService.showToast(err.message, 2000, "middle");
          event.target.complete();
        }
      );
    }
}

